<?php

class report extends Controller
{

	function __constructor()
	{
		parent::__constructor();
		
		
	}
	
	function index()
	{
		is_logged_in();
		$this->load->view("report/home");	
	}
	
	function client_hours()
	{
		
		is_logged_in();
		$this->load->model("model_report");
		$this->load->model("model_job");
		$match = "/^2[0-3]|[01][0-9]:[0-5][0-9]$/";
			
		$s_d = $this->input->post("report_start");
		$s_t = $this->input->post("report_start_time");
		$e_d = $this->input->post("report_end");
		$e_t = $this->input->post("report_end_time");
		
		$s = trim($s_d." ".$s_t.":00");
		$e = trim($e_d." ".$e_t.":59");
		
		if($this->input->post("action") != "")
		{


			if(
				( preg_match($match , $s) )
			 && ( preg_match($match , $e) )
			 )
			 {
			 	
			 	$r = $this->model_report->client_hours_logged(array($s,$e));
			 	$report = array();
			 	$report_parent = array();
			 	
			 	
			 	
			 	$c_id = array();
			 	
			 	//===========================================
			 	foreach($r->result_array() as $rr)
			 	{
			 		$report[0] = $rr;
			 		$job_details = array();
			 		
			 		//dump($rr);
			 		$job_where = array ("job.company_id" => $rr['id']
			 							,"job_start >" => $s
			 							,"job_end <" => $e
			 							);
			 							
			 							
			 		$r_subJobs = $this->model_job->getJobs(0,array("job.created", "desc"),$job_where);
			 		//dump($r_subJobs);
			 		
			 		foreach($r_subJobs as $ajob)
			 		{
			 			$job_details[] = $ajob;
			 		}
			 		
			 		$report[1] = $job_details;
			 		$report_parent[] = $report;
			 	}
			 	//===============
			 	
			 	
			 	
			 	
			 	$data['report_parent'] = $report_parent;
			 	$data['s'] = $s;
			 	$data['e'] = $e;
			 	
			 	$this->load->view("report/client_hours_per_client",$data);
			 }
		}
		else{
			$this->load->view("report/client_hours_per_client");
			
		}
	
	}
	
	function job_jobs()
	{
		is_logged_in();
		$this->load->model("model_job");
		$match = "/^2[0-3]|[01][0-9]:[0-5][0-9]$/";
			
		$s_d = $this->input->post("report_start");
		$s_t = $this->input->post("report_start_time");
		$e_d = $this->input->post("report_end");
		$e_t = $this->input->post("report_end_time");
		
		$s = trim($s_d." ".$s_t.":00");
		$e = trim($e_d." ".$e_t.":59");
		
		$data= array();
		$where = array();
		if($this->input->post("action") != "")
		{
			if(
				( preg_match($match , $s) )
			 && ( preg_match($match , $e) )
			 )
			 {
			 	$where = array ("job_start > " => $s, "job_end < " => $e);
			 }
		}
		
		$setPeriod = $this->uri->segment(3);
		if($setPeriod != "")
		{
			
			$e = date("Y-m-d H:i:s");
			switch($setPeriod)
			{
				case "7d":
				$s = date("Y-m-d H:i:s", strtotime("-7 days"));
				break;
				
				case "14d":
				$s = date("Y-m-d H:i:s", strtotime("-14 days"));
				break;
				
				case "1m":
				$s = date("Y-m-d H:i:s", strtotime("-1 month"));
				break;
				
				case "3m":
				$s = date("Y-m-d H:i:s", strtotime("-3 month"));
				break;
				
				case "6m":
				$s = date("Y-m-d H:i:s", strtotime("-6 month"));
				break;
				
				case "12m":
				$s = date("Y-m-d H:i:s", strtotime("-12 month"));
				break;
			}
			$where = array ("job_start > " => $s, "job_end < " => $e);
		}
		
		$r = $this->model_job->getJobs(0,array("job.created", "desc"),$where);
	//	dump($this->db->last_query());
		$data['jobs'] = $r;
		$data['e'] = $e;
		$data['s'] = $s;
		
		$this->load->view("report/job_logged_jobs",$data);
	}
	
	function user_hours()
	{
		is_logged_in();
		$this->load->model("model_report");
		$match = "/^2[0-3]|[01][0-9]:[0-5][0-9]$/";
			
		$s_d = $this->input->post("report_start");
		$s_t = $this->input->post("report_start_time");
		$e_d = $this->input->post("report_end");
		$e_t = $this->input->post("report_end_time");
		
		$s = trim($s_d." ".$s_t.":00");
		$e = trim($e_d." ".$e_t.":59");
		
		if($this->input->post("action") != "")
		{


			if(
				( preg_match($match , $s) )
			 && ( preg_match($match , $e) )
			 )
			 {
			 	$where = array (0 => $s, 1 => $e);
			 	
			 	$r = $this->model_report->user_hours($where);
			 	
//			 	dump($this->db->last_query());
			 	$data['jobs'] = $r->result_array();
			 	$data['e'] = $e;
			 	$data['s'] = $s;
			 	
			 	$this->load->view("report/user_hours",$data);
			 }
		
		}
		else{
			$this->load->view("report/user_hours");
		}
		
	}
	
	function client_view()
	{
		is_logged_in("user/login/", array(1,2,3));

		$this->load->model("model_job");
		$this->load->model("model_company");

		$client = $this->model_company->getClient($_SESSION['user_company']);
		$data['client'] = $client;
			
		$all = $this->uri->segment(2);
		if($all == "all" && $this->input->post("action") == "")
		{
			$where = array ( "job.company_id" => $_SESSION['user_company']);
			$r = $this->model_job->getJobs(0,array("job.created", "desc"),$where);	
			$data['jobs'] = $r;
			$data['msg'] = "All Jobs Logged ";
		}
		else{

			$match = "/^2[0-3]|[01][0-9]:[0-5][0-9]$/";
				
			$s_d = $this->input->post("report_start");
			$s_t = $this->input->post("report_start_time");
			$e_d = $this->input->post("report_end");
			$e_t = $this->input->post("report_end_time");

			$s = trim($s_d." ".$s_t.":00");
			$e = trim($e_d." ".$e_t.":59");



			if($this->input->post("action") != "")
			{


				if(
				( preg_match($match , $s) )
			 && ( preg_match($match , $e) )
			 )
			 {
			 	$where = array ("job_start > " => $s, "job_end < " => $e, "job.company_id" => $_SESSION['user_company']);
			 		
			 	$r = $this->model_job->getJobs(0,array("job.created", "desc"),$where);
			 		
			 	//		 	 	dump($this->db->last_query());

			 	$data['jobs'] = $r;
			 	$data['msg'] = "Jobs Logged  for the period of ' $s ' to ' $e '";
			 	
			 }
			}
		}
		$this->load->view("report/client_view",$data);
	}
}